import { resolve } from "path";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import UnoCSS from "unocss/vite";
import Components from "unplugin-vue-components/vite";
import AutoImport from "unplugin-auto-import/vite";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import viteCompression from "vite-plugin-compression";
import { visualizer } from "rollup-plugin-visualizer";

const env = loadEnv("development", "./");
const proxy_url = env.VITE_PROXY_URL;
// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      "@": resolve(__dirname, "src"),
    },
  },
  plugins: [
    UnoCSS(),
    vue(),
    AutoImport({
      imports: ["vue", "vue-router", "pinia"],
      dts: "src/auto-import.d.ts",
      eslintrc: {
        enabled: true,
      },
    }),
    Components({
      dirs: ["src/components", "src/pkg"],
      extensions: ["vue"],
      dts: "src/components.d.ts",
    }),
    createSvgIconsPlugin({
      iconDirs: [resolve(__dirname, "src/icons")],
      symbolId: "icon-[dir]-[name]",
    }),
    viteCompression(),
    visualizer(),
  ],
  server: {
    proxy: {
      "/api": {
        target: proxy_url,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
      "/avatars": {
        target: proxy_url,
        changeOrigin: true,
      },
      "/avatar": {
        target: proxy_url,
        changeOrigin: true,
      },
      "/captcha": {
        target: proxy_url,
        changeOrigin: true,
      },
    },
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          // bytemd: [
          //   "bytemd",
          //   "@bytemd/plugin-frontmatter",
          //   "@bytemd/plugin-gemoji",
          //   "@bytemd/plugin-gfm",
          //   "@bytemd/plugin-highlight",
          //   "@bytemd/plugin-math",
          //   "@bytemd/plugin-medium-zoom",
          //   "@bytemd/plugin-mermaid",
          //   "@bytemd/vue-next",
          // ],
          vditor: ["vditor"],
          chart: ["chart.js"],
        },
      },
    },
  },
});
